package com.zhiyou100.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.sun.mail.imap.protocol.ID;
import com.zhiyou100.entity.ResponseJson;
import com.zhiyou100.entity.TabDirectory;
import com.zhiyou100.service.ITabDirectoryService;
import lombok.ConfigurationKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 目录控制层
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-10 10:53
 */
@RestController
public class DirectoryController {
    @Autowired
    private ITabDirectoryService service;

    @GetMapping("/directory/open.action")
    public ResponseJson<List<TabDirectory>> openMethod(Integer did, HttpServletRequest request) {
        String template = "打开文件夹，当前处理请求的服务器的地址 {}，Session 中的 user 信息 {} ";
        // 当前处理请求的服务器
        final String localAddr = request.getLocalAddr();
        //Session  中的 user 信息
        final Object user = request.getSession().getAttribute("user");
        // 格式化后的字符串
        final String format = StrUtil.format(template, localAddr, user);
        return new ResponseJson<>(200, format, service.listByDid(did));
    }

    @PostMapping("/directory/add.action")
    public ResponseJson<String> addMethod(TabDirectory tabDirectory) {
        service.addDirectory(tabDirectory);
        return new ResponseJson<>(200, "添加文件成功！", null);
    }


    @DeleteMapping("/directory/delete/{did}.action")
    public ResponseJson<String> directoryMethod(@PathVariable("did") Integer did) {
        Console.error("删除方法 did = " + did);
        service.deleteOneByDid(did);
        return new ResponseJson<>(200, "删除文件夹成功！", null);
    }


}
